/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Project2.cs342.project1.mathNotebook.Database;

import java.sql.*;
/**
 *
 * @author Rick
 */
public class DBGroups {
    static String id = null;
    static String file_name = null;
    static Connection conn = null;
    PreparedStatement ps = null;
    SQLConfigeration sqlConfig;

    public DBGroups(){
       sqlConfig = new SQLConfigeration();
       conn = sqlConfig.getConnection();
    }

    /**
     * inserts a group and its access level
     *
     * @param groupName the name of the group
     * @param accessLevel the access level of the provided group
     */
    public void insertIntoGroups(String groupName, String accessLevel){

        //Insert data
        String groupAccess = "insert into Groups(groupname,accessLevel)" +
                                                    "values (?,?)";
        try{
            conn.setAutoCommit(false);

            ps = conn.prepareStatement(groupAccess);

            ps.setString(1, groupName);
            ps.setString(2, accessLevel);
            System.out.println("Attempting insert");
            ps.executeUpdate();
            conn.commit();
            System.out.println("Insert successful");

        }catch(Exception ex){
            System.out.println("Insert Error " + ex + " at:");
            ex.printStackTrace();
        }finally{
            try {
                ps.close();

            } catch (SQLException ex) {
                System.out.println("Sql Error " + ex);
            }
        }
    }

    public String retrieveGroupAccessLevel(String groupName){
        String accessLevel = null;

        ResultSet rs = null;
        PreparedStatement pstmt = null;
        String query = "Select accessLevel from Groups where groupname = ?";

        try{
            pstmt = conn.prepareStatement(query);

            pstmt.setString(1, groupName);
            rs = pstmt.executeQuery();

            rs.next();

            System.out.println("Attempting to get accessLevel");

            accessLevel = rs.getString("accessLevel");

            System.out.println("Recieved successful");


        }catch(Exception ex)
        {
            System.out.println("Retrieval Error: " + ex);
            //ex.printStackTrace();
        }
        finally{
            try {
                rs.close();
                pstmt.close();
                //conn.close();
            } catch (SQLException ex) {
                System.out.println("SQL Error: " + ex);
            }
        }
        return accessLevel;
    }

    /**
     * Used to modifie the access level of a given group
     *
     * @param groupName name of group to have access level modified
     * @param accessLevel the access level to be changed to
     */
    public void updateAccessLevel(String groupName, String accessLevel){
        String updateGroup = "update Groups set accessLevel = '" +
                                                    accessLevel + "' where  groupname = '" +
                                                    groupName + "'";
        try{
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(updateGroup);
            System.out.println("Attempting Update");
            ps.executeUpdate();
            conn.commit();
            System.out.println("Update Successful");

        }catch(SQLException ex){
            System.out.println("Update Error: " + ex);
        }

    }

    /**
     * Deletes the provided group from the table
     *
     * @param groupName the group to be deleted
     */
    public void deleteGroup(String groupName){
        String deleteGroup = "delete from Groups where groupName = '" +
                                                    groupName + "'";
        try{
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(deleteGroup);
            System.out.println("Attempting Deletion");
            ps.executeUpdate();
            conn.commit();
            System.out.println("Delection Successful");

        }catch(SQLException ex){
            System.out.println("Deletion Error: " + ex);
        }

    }

}
